package the.lb.workbench.setting.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.context.UserContext;
import the.broccoli.setting.entity.Dict;
import the.broccoli.setting.service.IDictService;
import the.broccoli.setting.vo.DictVO;

import java.util.Date;
import java.util.List;

/**
 * 字典项控制器
 *
 * @author lei.liu
 * @since 2019-11-27 16:56:32
 */
@RestController
@RequestMapping("/setting/dict")
public class DictController {

    @Autowired
    private IDictService dictService;

    @PostMapping("/add")
    public APIResult add(
    ) {
        Dict entity = new Dict();
        entity.setCreatedId(UserContext.userId());
        entity.setCreatedName(UserContext.userName());
        entity.setCreatedTime(new Date());
        dictService.save(entity);
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        dictService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(
    ) {
        Dict entity = new Dict();
        entity.setModifiedId(UserContext.userId());
        entity.setModifiedName(UserContext.userName());
        entity.setModifiedTime(new Date());
        dictService.updateById(entity);
        return APIResult.success();
    }

    @GetMapping("/{dictId}")
    public APIResult get(@PathVariable(value = "dictId") String dictId) {
        return APIResult.success(dictService.getById(dictId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult<Dict>> page(
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        DictVO vo = new DictVO();
        vo.setKeyword(keyword);
        vo.setOrderBy(orderBy);
        return APIResult.success(dictService.pageList(vo, pageNum, pageSize));
    }
}